package com.redis.lesson.web;

import com.redis.lesson.service.ProductServiceImpl;
import com.redis.lesson.vo.req.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 阿甘
 * @see https://study.163.com/provider/1016671292/course.htm?share=1&shareId=1016481220
 * @version 1.0
 * 注：如有任何疑问欢迎阿甘老师微信：agan-java 随时咨询老师。
 */
@RestController
@Slf4j
@RequestMapping(value = "/pruduct")
@Api("商品模块")
public class ProductController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ProductServiceImpl productService;

    @PostMapping(value = "/create")
    @ApiOperation("增加商品")
    public String create(ProductVO obj) {
        Product product = new Product();
        BeanUtils.copyProperties(obj,product);
        String message = productService.addProduct(obj);
        return message;
    }

    @PostMapping(value = "/getProduct")
    @ApiOperation("查找商品")
    public Map<String, Object> getProduct(long id) {
        Product product = new Product();
        Map<String, Object> map = productService.getProduct(id);
        return map;
    }

    @PostMapping(value = "/addPrice")
    @ApiOperation("加个变动")
    public void addPrice(int id,int price) {
         productService.addPrice(id,price);
    }

    /**
     * 将Object对象里面的属性和值转化成Map对象
     */
    public  Map<String, Object> objectToMap(Object obj)  {
        Map<String, Object> map = new HashMap<String,Object>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                log.error(e.getMessage());
            }
            map.put(fieldName, value);
        }
        return map;
    }
}












